package ch.fhnw.qualiscan.application.pages.tabs;

import java.util.List;

import ch.fhnw.qualiscan.Config;
import ch.fhnw.qualiscan.application.pages.Admin;
import ch.fhnw.qualiscan.core.db.pojos.Stage;
import ch.fhnw.qualiscan.core.db.pojos.Survey;
import ch.fhnw.qualiscan.core.libraries.NotificationLib;

import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Component;
import com.vaadin.ui.Label;
import com.vaadin.ui.Table;
import com.vaadin.ui.VerticalLayout;

import eu.livotov.tpt.TPTApplication;
import eu.livotov.tpt.gui.dialogs.OptionDialog;
import eu.livotov.tpt.gui.dialogs.OptionKind;
import eu.livotov.tpt.i18n.TM;

/**
 * Edit surveys
 *
 */
@SuppressWarnings("serial")
public class AdminTab4 extends VerticalLayout{

	public Admin admin;
	private Table surveyTable;
	public BeanItemContainer<Survey> tableContainer ;
	
	public AdminTab4(Admin admin) {
		this.admin = admin;
		
		buildLayout();
	}
	
	public void buildLayout() {
		removeAllComponents();
		
		VerticalLayout tableWrap = new VerticalLayout();
		tableWrap.addComponent(buildTable());
		tableWrap.setMargin(true);
		addComponent(tableWrap);
	}
	
	public Table buildTable() {
		tableContainer = new BeanItemContainer<Survey>(Survey.class);
		
		List<Survey> surveyList = admin.adminModel.getSurveys();
		for(int i = 0; i < surveyList.size(); i++) {
			tableContainer.addBean(surveyList.get(i));
		}
		
		surveyTable = new Table();
		surveyTable.setWidth("100%");
		surveyTable.setPageLength(0);
		surveyTable.setSelectable(true);
		
		surveyTable.addGeneratedColumn("stageTitle", new Table.ColumnGenerator() {
			public Component generateCell(Table source, Object itemId, Object columnId) {
				Survey survey = (Survey) itemId;
				Stage stage = survey.getStage();
			
				Label stageTitle = new Label(admin.surveyModel.getStageText(stage, admin.userSession.getLanguage()).getTitle());
				return stageTitle;
			}
		});
		
		surveyTable.addGeneratedColumn("surveyDeleteButton", new Table.ColumnGenerator() {
			public Component generateCell(Table source, final Object itemId, Object columnId) {
 				Button deleteButton = new Button(TM.get("action.delete"), new Button.ClickListener() {
					public void buttonClick(ClickEvent event) {
						final OptionDialog dlg = new OptionDialog (TPTApplication.getCurrentApplication ());
						dlg.showConfirmationDialog(TM.get("admin.tab4.surveydelete"), TM.get("admin.tab4.surveydelete.desc", ((Survey) itemId).getTitle()), new OptionDialog.OptionDialogResultListener () {
						  public void dialogClosed(OptionKind closeEvent) {
							  if(closeEvent == OptionKind.OK) {
								  NotificationLib.showSuccess(((Survey)itemId).getTitle() + " " + TM.get("action.deleted"), null);
								  tableContainer.removeItem(itemId);
								  admin.adminModel.deleteSurvey((Survey) itemId);
							  }
						  }
						});
					}
				});
 				deleteButton.setData(itemId);
				deleteButton.setIcon(Config.ICON_CANCEL);
				
				return deleteButton;
			}
		});
		
		surveyTable.setContainerDataSource(tableContainer);
		
		surveyTable.setColumnHeader("title", TM.get("admin.surveytable.title"));
		surveyTable.setColumnHeader("stageTitle", TM.get("admin.surveytable.stage"));
		surveyTable.setColumnHeader("creationdate", TM.get("admin.surveytable.creationdate"));
		surveyTable.setColumnHeader("completiondate", TM.get("admin.surveytable.completiondate"));
		surveyTable.setColumnHeader("surveyDeleteButton", TM.get("action.delete"));
		
		surveyTable.setVisibleColumns(new Object[]{"title", "stageTitle", "creationdate", "completiondate", "surveyDeleteButton"});
		
		return surveyTable;
	}
}
